package com.longcai.controller.trad;


import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.nearby.ClyjTradingArea;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.trad.ClyjTradingAreaService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商圈表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-04-11
 */
@RestController
@RequestMapping("/clyjTradingArea")
public class ClyjTradingAreaController {

    @Autowired
    private ClyjTradingAreaService clyjTradingAreaService;

    /**
     * 获取商圈列表
     *
     * @param clyjTradingArea
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "商圈列表")
    @GetMapping(value = "/tradList")
    public PageTableResponse tradList(ClyjTradingArea clyjTradingArea, PageHelper pageHelper) {

        return this.clyjTradingAreaService.tradList(clyjTradingArea, pageHelper);
    }

    /**
     * 新增
     *
     * @param clyjTradingArea
     * @return
     */
    @PostMapping(value = "/saveTrad")
    @ApiOperation(value = "新增商圈")
    @PreAuthorize("hasAuthority('trading:add')")
    public ClyjTradingArea saveHead(ClyjTradingArea clyjTradingArea, String s_province, String s_city, String s_county) {
        ClyjTradingArea u = this.clyjTradingAreaService.getName(clyjTradingArea.getName());
        if (u != null) {
            throw new IllegalArgumentException(clyjTradingArea.getName() + "已存在");
        }
        //判断id
        if (clyjTradingArea.getId() == null || clyjTradingArea.getId().equals("")) {
            clyjTradingArea.setSProvince(s_province);
            clyjTradingArea.setSCity(s_city);
            clyjTradingArea.setSCounty(s_county);
            return clyjTradingAreaService.saveHead(clyjTradingArea);
        } else {
            clyjTradingArea.setSProvince(s_province);
            clyjTradingArea.setSCity(s_city);
            clyjTradingArea.setSCounty(s_county);
            this.clyjTradingAreaService.updateHead(clyjTradingArea);
        }

        return null;
    }

    /**
     * 根据id获取商圈信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取商圈信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('trading:query')")
    public ApiResult user(@PathVariable String id) {

        return this.clyjTradingAreaService.getById(id);
    }

    /**
     * 修改商圈信息
     *
     * @param clyjTradingArea
     * @return
     */
    @PutMapping(value = "updateTrading")
    @ApiOperation(value = "修改商圈信息")
    @PreAuthorize("hasAuthority('trading:update')")
    public Integer updateHeadline(@RequestBody ClyjTradingArea clyjTradingArea) {
        return this.clyjTradingAreaService.updateHead(clyjTradingArea);
    }

    /**
     * 删除商圈
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除商圈")
    @PreAuthorize("hasAuthority('trading:delete')")
    public void delete(@PathVariable String id) {

        this.clyjTradingAreaService.deleteById(id);
    }
}

